package com.kaifamiao.xml.d0930;

import com.kaifamiao.xml.d0929.Student;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class Demo01 {

    public static void main(String[] args) {
        InputStream inputStream = Demo01.class.getClassLoader().getResourceAsStream("mapper.xml");

        SAXReader reader = new SAXReader();

        try {
            Document read = reader.read(inputStream);

            Element sqlElement = read.getRootElement();

            Element select = sqlElement.element("select");

            String sql = select.getTextTrim();
            String type = select.attributeValue("type");

            Class cla = Class.forName(type);

            query(sql, cla);

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void query(String sql, Class cla){
        List<Student> list = new DBUtil().query(sql, cla);

        for (Student student : list) {
            System.out.println(student);
        }
    }
}
